//
//  SearchBarViewController.m
//  AudioBook_A
//
//  Created by Sansan on 15/4/13.
//  Copyright (c) 2015年 Sansan. All rights reserved.
//

#import "SearchBarViewController.h"

@interface SearchBarViewController ()

@end

@implementation SearchBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *searchView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 35)];
    self.searchBar = [[UITextField alloc] init];
    self.searchBar.backgroundColor = [UIColor whiteColor];
    self.searchBar.frame = CGRectMake(0, 5, 200, 25);
    self.searchBar.borderStyle = UITextBorderStyleRoundedRect;
    self.searchBar.layer.cornerRadius = 12.5;
    self.searchBar.clearButtonMode = UITextFieldViewModeAlways;
  
    UIImageView *leftView = [[UIImageView alloc] init];
    leftView.image = [UIImage imageNamed:@"iconfont-sousuo-2.png"];
    leftView.frame = CGRectMake(2, 2, 20, 20);
    self.searchBar.leftView = leftView;
    //设置leftViewMode
    self.searchBar.leftViewMode=UITextFieldViewModeAlways;
    //设置放大镜距离左边的间距，设置leftView的内容居左
    leftView.contentMode=UIViewContentModeCenter;
    self.searchBar.placeholder = @"专辑, 音乐";
    //设置文字内容垂直居中
    self.searchBar.textAlignment = NSTextAlignmentLeft;
    
    self.navigationItem.titleView = searchView;
    [searchView addSubview:self.searchBar];
    
    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    searchButton.frame = CGRectMake(200, 0, 60, 34);
    [searchButton setTitle:@"搜索" forState:UIControlStateNormal];
    [searchButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    [searchView addSubview:searchButton];
    
    // 键盘回收签订协议
    self.searchBar.delegate =self;
    
//    // 添加手势
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
//    [self.view addGestureRecognizer:tap];
//    [tap release];
    
    self.seachTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenFram.width, ScreenFram.height - 64) style:UITableViewStylePlain];
    self.seachTableView.delegate = self;
    self.seachTableView.dataSource = self;
    [self.view addSubview:self.seachTableView];
    [_seachTableView release];
    
    self.searchView = [[SearchView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:self.searchView];
    [_searchBar release];
    
    [self.searchView.hotUIbutton1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.searchView.hotUIbutton1.tag = 100001;
    [self.searchView.hotUIbutton2 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.searchView.hotUIbutton1.tag = 100002;
    [self.searchView.hotUIbutton3 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.searchView.hotUIbutton1.tag = 100003;
    [self.searchView.hotUIbutton4 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.searchView.hotUIbutton1.tag = 100004;
    [self.searchView.hotUIbutton5 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.searchView.hotUIbutton1.tag = 100005;
    [self.searchView.hotUIbutton6 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.searchView.hotUIbutton1.tag = 100006;
    [self.searchView.hotUIbutton7 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.searchView.hotUIbutton1.tag = 100007;
    [self.searchView.hotUIbutton8 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.searchView.hotUIbutton1.tag = 100008;
    [self.searchView.hotUIbutton9 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.searchView.hotUIbutton1.tag = 100009;
    
    [MyAFNData getNetData:@"http://mobile.ximalaya.com/m/hot_search_keys?device=iPhone" responseGETData:^(id data) {
        
        NSMutableArray *array = [data objectForKey:@"keys"];

        [self.searchView.hotUIbutton1 setTitle:[array objectAtIndex:0] forState:UIControlStateNormal];
        [self.searchView.hotUIbutton2 setTitle:[array objectAtIndex:1] forState:UIControlStateNormal];
        [self.searchView.hotUIbutton3 setTitle:[array objectAtIndex:2] forState:UIControlStateNormal];
        [self.searchView.hotUIbutton4 setTitle:[array objectAtIndex:3] forState:UIControlStateNormal];
        [self.searchView.hotUIbutton5 setTitle:[array objectAtIndex:4] forState:UIControlStateNormal];
        [self.searchView.hotUIbutton6 setTitle:[array objectAtIndex:5] forState:UIControlStateNormal];
        [self.searchView.hotUIbutton7 setTitle:[array objectAtIndex:6] forState:UIControlStateNormal];
        [self.searchView.hotUIbutton8 setTitle:[array objectAtIndex:7] forState:UIControlStateNormal];
        [self.searchView.hotUIbutton9 setTitle:[array objectAtIndex:8] forState:UIControlStateNormal];
    }];
    
    self.seachArray = [NSMutableArray array];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.seachArray.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"找专辑";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    AudioListModel *model = [self.seachArray objectAtIndex:indexPath.row];
    
    cell.textLabel.text = model.title;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.searchBar resignFirstResponder];
    
    DetailAlbumsViewController *audioVC = [[DetailAlbumsViewController alloc] init];
    NSString *myID = ((AudioListModel *)[self.seachArray objectAtIndex:indexPath.row]).id;
    // 属性传值把id传递给下一个页面
    NSString *title = ((AudioListModel *)[self.seachArray objectAtIndex:indexPath.row]).title;
    
    audioVC.ID = myID;
    audioVC.title = title;
    [self.navigationController pushViewController:audioVC animated:YES];
    [audioVC release];
}



- (void)searchAction
{
    [self.searchBar resignFirstResponder];
    self.seachArray = [NSMutableArray array];
    [MyAFNData getNetData:[NSString stringWithFormat:@"http://mobile.ximalaya.com/s/search/suggest?device=iPad&keywords=%@&scope=all", self.searchBar.text] responseGETData:^(id data) {
        
        NSMutableArray *albumArray = [[data objectForKey:@"album"] objectForKey:@"list"];
        
        if (albumArray.count > 0) {
            [self.view bringSubviewToFront:self.seachTableView];
            for (NSMutableDictionary *dic in albumArray) {
                AudioListModel *model = [[AudioListModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.seachArray addObject:model];
                [model release];
            }
            [self.seachTableView reloadData];
            
        } else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"未搜索到相关内容!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alertView show];
            [alertView release];
        }
        
    }];
}

- (void)buttonAction:(UIButton *)button
{
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.searchBar resignFirstResponder];
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
